package javaguide.设计模式.单例模式;

public class Singleton {
    //懒汉式 线性不安全
/*    private static Singleton instance;
    private Singleton(){};
    public static Singleton getInstance(){
        if (instance == null){
            instance = new Singleton();
        }
        return instance;
    }
    */

    //懒汉式 线性安全
    //加了锁
   /* private static Singleton instance;
    private Singleton(){};
    public static synchronized Singleton getInstance(){
        if (instance == null){
            instance = new Singleton();
        }
        return instance;
    }
*/

 /*  //饿汉式
    private static  Singleton instance = new Singleton();
    private Singleton(){}
    public static Singleton getInstance(){
    return instance;
    }*/

  //双检锁/双重校验锁（DCL，即 double-checked locking）
 private volatile static Singleton singleton;
 private Singleton(){}
 public static Singleton getSingleton(){
     if (singleton == null){
         synchronized (Singleton.class){
             if (singleton == null){
                 singleton = new Singleton();
             }
         }
     }
     return singleton;
 }


}
